import { message } from 'antd';
import {
  searchShortcutList,
  searchWaitTask,
  addFunction,
  searchNotice,
  searchFunParams,
} from '@/services/workplace';

export default {
  state: {
    shortcutChosen: [],
    shortcutList: [],
    waitTaskData: [],
    shortcutData: [],
    noticeData: [],
    chosenKeys: [],
  },
  effects: {
    // 新增快速开始
    *addFunction({ payload }, { call }) {
      const res = yield call(addFunction, payload);
      return res;
    },
    // 查找工作台项目
    *searchWaitTask(_, { call, put, select }) {
      const res = yield call(searchWaitTask);
      const menuData = yield select(state => state.menu.breadcrumbNameMap);
      if (res.success) {
        yield put({
          type: 'updateWaitTask',
          payload: {
            data: res.data,
            menuData,
          },
        });
      } else {
        message.warning(res.message || '获取数据失败');
      }
    },
    // 查找快速开始项目
    *searchFunParams(_, { call, put, select }) {
      const res = yield call(searchFunParams);
      const menuData = yield select(state => state.menu.breadcrumbNameMap);
      if (res.success) {
        yield put({
          type: 'updateFunParams',
          payload: {
            data: res.data,
            menuData,
          },
        });
      } else {
        message.warning(res.message || '获取数据失败');
      }
      return res;
    },
    // 查找公告
    *searchNotice(_, { call, put }) {
      const res = yield call(searchNotice);
      if (res.success) {
        yield put({
          type: 'updateModalData',
          payload: {
            noticeData: res.data,
          },
        });
      } else {
        message.warning(res.message || '获取数据失败');
      }
    },
    // 查找快速开始中的项目
    *searchShortcutList({ payload }, { call, put }) {
      const res = yield call(searchShortcutList, payload);
      if (res.success) {
        yield put({
          type: 'searchShortcutData',
          payload: res.data,
        });
      } else {
        message.warning(res.message || '获取数据失败');
      }
    },
  },
  reducers: {
    // 更改快速开始中的项目
    searchShortcutData(state, { payload }) {
      const chosenKeys =
        payload.querUserFunction && payload.querUserFunction.map(item => item.funId);
      return {
        ...state,
        shortcutChosen: payload.querUserFunction,
        shortcutList: payload.querUserSencondPerms,
        chosenKeys,
      };
    },
    // 更改工作台项目
    updateWaitTask(
      state,
      {
        payload: { data, menuData },
      }
    ) {
      const tempMenu = [];
      Object.keys(menuData).forEach(key => {
        tempMenu.push(menuData[key]);
      });
      const deepFindPath = (authority, menu) => {
        let result = {};
        menu.forEach(item => {
          if (item.authority && item.authority.includes(authority)) {
            result = item;
          } else if (item.children) {
            deepFindPath(authority, item.children);
          }
        });
        return result.path;
      };
      const waitList = JSON.parse(JSON.stringify(data));
      waitList.forEach(item => {
        const temp = item;
        temp.url = deepFindPath(temp.permsCode, tempMenu);
      });
      return {
        ...state,
        waitTaskData: waitList,
      };
    },
    // 更新快速开始项目
    updateFunParams(
      state,
      {
        payload: { data, menuData },
      }
    ) {
      const tempMenu = [];
      Object.keys(menuData).forEach(key => {
        tempMenu.push(menuData[key]);
      });
      const deepFindPath = (authority, menu) => {
        let result = {};
        menu.forEach(item => {
          if (item.authority && item.authority.includes(authority)) {
            result = item;
          } else if (item.children) {
            deepFindPath(authority, item.children);
          }
        });
        return result.path;
      };
      const userFunctionList = JSON.parse(JSON.stringify(data));
      userFunctionList.forEach(item => {
        const temp = item;
        temp.url = deepFindPath(item.permsCodes[0], tempMenu);
      });
      return {
        ...state,
        shortcutData: userFunctionList,
      };
    },
    updateModalData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
